/*
 * SpiderpigOS
 * Copyright (C) 2009 - 2010 Davide Gessa
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <mm/mm.h>
#include <mm/phys.h>
#include <config.h>


/* initialize memory manager */
int
mm_init(uint64_t mem)
{
	phys_init(mem);
	
	return 0;
}



/* alloc a block */
void *
mm_alloc(size_t size)
{	
	unsigned bnum = (unsigned) ((size + 1) / (PHYS_PAGE_SIZE * 1024)) + 1; 
	// / PHYS_PAGE_SIZE + 1;
	return (void *)phys_alloc(bnum);
}


/* free a block */
void 
mm_free(void *addr)
{
	phys_free(addr);
}


/* return total memory */
uint32_t 
mm_get_total_mem()
{ 
	return phys_total;
}


/* return used memory */
uint32_t 
mm_get_used_mem()
{ 
	return ((phys_page_number - phys_get_free_pages()) * PHYS_PAGE_SIZE);
}



/* get allocation size */
uint32_t 
mm_get_usable_size(void *addr)
{
	int i = 0;
	for(i = 0; i < PHYS_MAX_BLOCKS; i++)
		if(phys_allocated[i].address == addr)
			return phys_allocated[i].dim;
	
	return 0;
}
